<?php

class SelectWiele2
{

   private $dane = array();
   private $count;
   
   /**
    * 
    * Enter description here ...
    * @param unknown_type $p_zapytanie
    * @param unknown_type $p_mapery
    * @param unknown_type $p_ilosc_rekordow
    * @param unknown_type $p_numer_strony
    * return_type
    */
   public function ZapytajBaze($p_zapytanie, $p_mapery = array(), $p_ilosc_rekordow, $p_numer_strony)
   {
       $this->select = $p_zapytanie;
       $db = Zend_Db_Table_Abstract::getDefaultAdapter();

       //-----Wycinanie nazwy tabeli z wybieranych kolumn-------//
       foreach ($p_mapery as $m) {
           $maper = new $m();
           
           $p_zapytanie = $maper->renderujKolumny($p_zapytanie);
       }
     
       //-----Wykonywanie zapytania-----------------------------//
        
        
       $zapytanie = $db->query($p_zapytanie.' LIMIT '.$p_ilosc_rekordow.' OFFSET '.($p_ilosc_rekordow*($p_numer_strony-1)));
       
       $zapytanie->setFetchMode(Zend_Db::FETCH_NAMED);
       $result = $zapytanie->fetchAll();

       $ex = new Zend_Db_Expr('('.$p_zapytanie.')');//('('.substr($p_zapytanie,0,strpos($p_zapytanie, 'LIMIT')).')');
       $this->count = $db->select()->from($ex);

       $array_temp = array();
       $array_wynik = array();

       foreach ($result as $v => $s) {

           foreach ($s as $v2 => $s2) {

               if (is_array($s2))
               {
                   for ($licznik = 0; $licznik < count($p_mapery); $licznik++) {
                       $maper = new $p_mapery[$licznik];

                       foreach ($maper->fields_list as $kol => $def)
                       {
                           if ($maper->renderujKolumny2($v2)==$kol)
                           {
                               $array_temp[$maper->_name.'_'.$v2] = $s2[$licznik];
                           }
                       }
                   }
               }
               else
               {
                   foreach ($p_mapery as $m)
                   {
                       $maper = new $m;
                       foreach ($maper->fields_list as $kol => $def)
                       {
                           if ($maper->renderujKolumny2($v2)==$kol)
                           {
                               $array_temp[$maper->_name.'_'.$v2] = $s2;
                           }
                       }
                   }
               }

               foreach ($p_mapery as $m)
               {
                   $maper = new $m;
                   foreach ($maper->fields_list as $kol => $def)
                   {
                       if ($maper->renderujKolumny2($v2)<>$kol)
                       {
                           $array_temp[$v2] = $s2;
                       }
                   }
               }
           }
           $this->dane[] = $array_temp;
       }

       return $this->dane;

   }

   function dane()
   {
       return $this->dane;
   }

   function rowCount()
   {
     return $this->count;
   }
}